﻿using System;

namespace Com.OpenCode.Framework.Core
{
    /// <summary>
    ///     日志记录服务类。
    /// </summary>
    public static class LogServices
    {
        public static event LogEventDelegate ListenLog;

        /// <summary>
        ///     输出日志到Log4Net
        /// </summary>
        /// <param name="msg">异常信息。</param>
        /// <param name="type"></param>
        public static void WriteError(string msg, Type type = null,bool show=true)
        {
            WriteLog(msg, LogType.Error, type, show);
        }

        /// <summary>
        ///     输出日志到Log4Net
        /// </summary>
        /// <param name="msg">异常信息。</param>
        /// <param name="type"></param>
        public static void WriteInfo(string msg, Type type = null, bool show = true)
        {
            WriteLog(msg, LogType.Info, type, show);
        }

        /// <summary>
        ///     输出日志到Log4Net
        /// </summary>
        /// <param name="msg">异常信息。</param>
        /// <param name="type"></param>
        public static void WriteWarn(string msg, Type type = null, bool show = true)
        {
            WriteLog(msg, LogType.Warn, type, show);
        }

        private static void WriteLog(string msg, LogType logType, Type type = null, bool show = true)
        {
            LogRecord _LogRecord = new LogRecord
            {
                DateTime = DateTime.Now,
                Info = msg,
                Type = logType
            };
            if (ListenLog != null)
            {
                ListenLog(new LogEventArgs {LogRecord = _LogRecord, Type = type,Show = show});
            }
        }
    }
}